package com.example.rumen;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class gridview extends AppCompatActivity {
    int imags[] = new int[]{R.drawable.qq1, R.drawable.wangyiyun, R.drawable.bilibili, R.drawable.voa, R.drawable.weixin2,
            R.drawable.ximalaya, R.drawable.taobao22, R.drawable.qq1, R.drawable.wangyiyun, R.drawable.bilibili, R.drawable.voa,
            R.drawable.weixin2, R.drawable.ximalaya};
    String names[] = new String[]{"QQ", "网易云音乐", "哔哩哔哩", "VOA", "微信", "喜马拉雅", "淘宝",
            "QQ", "网易云音乐", "哔哩哔哩", "VOA", "微信", "喜马拉雅"};
    private GridView grid_view;
    private ArrayList<Map<String, Object>> arrayList;
    private SimpleAdapter simpleAdapter;
    private Intent intent;
    private Uri uri;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_gridview);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //1.装备框架
        grid_view = findViewById(R.id.gridView_0325);
        //2.装备数据，布局文件
        arrayList = new ArrayList<>();
        for (int i = 0; i < imags.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("001", imags[i]);
            map.put("002", names[i]);
            arrayList.add(map);
        }
        //3.准备布局文件
        //4.实现数组适配器
        simpleAdapter = new SimpleAdapter(getApplicationContext(), arrayList, R.layout.grid,
                new String[]{"001", "002"},
                new int[]{R.id.imageView_0325_zm, R.id.textView_0325_zm});
        //5.加载适配器
        grid_view.setAdapter(simpleAdapter);
        grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent(Intent.ACTION_VIEW);
                switch (position) {
                    case 0:
                        uri = Uri.parse("https://www.qq.com/");
                        intent.setData(uri);//TODO
                        startActivity(intent);//TODO
                        break;
                    case 1:
                        uri = Uri.parse("https://music.163.com/");
                        intent.setData(uri);
                        startActivity(intent);
                        break;
                    case 2:
                        uri = Uri.parse("https://www.bilibili.com/");
                        intent.setData(uri);
                        startActivity(intent);
                        break;
                    case 3:
                        uri = Uri.parse("https://www.bilibili.com/");
                        intent.setData(uri);
                        startActivity(intent);
                        break;
                    case 4:
                       Intent intent1 = new Intent(getApplicationContext(), Appshop.class);
                       startActivity(intent1);
                       break;

                }
                Toast.makeText(getApplicationContext(), "您当前选择的是" + names[position], Toast.LENGTH_SHORT).show();
            }
        });
    }
}